/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package conf2.ui;

import conf2.model.EActor.Side;
import conf2.model.ESpecialCard;
import conf2.text.Cards;
import conf2.text.Misc;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.LinkedList;
import java.util.List;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

/**
 *
 * @author Administrator
 */
class SelectableImagePanel extends ImagePanel {

    private boolean isSelected = false;

    public SelectableImagePanel(Image image) {
        super(image);
    }

    public void setSelected(boolean b) {
        if (b != isSelected) {
            isSelected = b;
            repaint();
        }
    }

    public boolean isSelected() {
        return isSelected;
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.setColor(Color.WHITE);
        g.drawRect(0, 0, getWidth()-1, getHeight()-1);
        if (isSelected) {
            Image tick = Images.get(Images.Which.GREEN_TICK);
            g.drawImage(tick, getWidth() - tick.getWidth(null) - 3, getHeight() - tick.getHeight(null) - 3, null);
        }
    }
}

public class SpecialCardChooserDialog extends JDialog {

    final List<ESpecialCard> selected = new LinkedList<ESpecialCard>();
    final JButton okButton = new JButton(Misc.OK_LABEL);

    public ESpecialCard[] getSelectedCards() {
        ESpecialCard[] cards = new ESpecialCard[selected.size()];
        return selected.toArray(cards);
    }

    public SpecialCardChooserDialog(Frame parent, Side side, final int howMany) {
        super(parent, true);
        setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);

        assert howMany > 0 && howMany < 5;

        ESpecialCard[] cards;
        Color bg, fg;
        if (side == Side.LIGHT) {
            cards = new ESpecialCard[]{ESpecialCard.A_KING_REVEALED, ESpecialCard.GANDALF_THE_WHITE, ESpecialCard.GWAIHIR_THE_WINDLORD, ESpecialCard.SHADOWFAX};
            bg = ColorSet.LIGHTCARD_BG;
            fg = ColorSet.LIGHTCARD_TEXT;
        } else {
            bg = ColorSet.DARKCARD_BG;
            fg = ColorSet.DARKCARD_TEXT;
            cards = new ESpecialCard[]{ESpecialCard.CREBAIN_OF_DUNLAND, ESpecialCard.PALANTIR, ESpecialCard.RECALL_TO_MORDOR, ESpecialCard.THE_DARK_OF_MORDOR};
        }

        JPanel p = new JPanel(new BorderLayout(0, 20));
        p.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
        p.setBackground(bg);

        setTitle("Special card selection");

        JPanel cardPanel = new JPanel(new GridLayout(1, howMany, 20, 0));
        cardPanel.setBackground(bg);

        for (final ESpecialCard c : cards) {
            final SelectableImagePanel ip = new SelectableImagePanel(Images.getSpecialCard(c));
            ip.setHoverSensitive(true);
            ip.setToolTipText("<html>" + Cards.getSpecialCardDescription(c) + "</html>");
            ip.addMouseListener(new MouseAdapter() {

                @Override
                public void mousePressed(MouseEvent e) {
                    if (ip.isSelected()) {
                        selected.remove(c);
                        ip.setSelected(false);
                        okButton.setEnabled(false);
                    } else {
                        if (selected.size() < howMany) {

                            selected.add(c);
                            ip.setSelected(true);
                            if (selected.size() == howMany) {
                                okButton.setEnabled(true);
                            }
                        }
                    }
                }
            });
            cardPanel.add(ip);
        }

        JLabel msgLabel = new JLabel("<html>Please choose " + howMany + " special cards to use in this round.</html>");
        msgLabel.setVerticalAlignment(SwingConstants.CENTER);
        msgLabel.setHorizontalAlignment(SwingConstants.CENTER);
        msgLabel.setFont(FontSet.getActorviewerText());
        msgLabel.setForeground(fg);

        p.add(msgLabel, BorderLayout.NORTH);

        p.add(cardPanel, BorderLayout.CENTER);
        JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
        buttonPanel.setBackground(bg);
        buttonPanel.add(okButton);

        p.add(buttonPanel, BorderLayout.SOUTH);

        setContentPane(p);

        pack();

        setLocationRelativeTo(parent);

        okButton.setEnabled(false);
        final JDialog dlg = this;
        okButton.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                dlg.setVisible(false);
            }
        });
    }
}
